﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Web.Configuration;
using System.Net.Mail;
using System.Net;
using System.Web.UI.WebControls;

/// <summary>
/// Descrizione di riepilogo per UtilsAdmin
/// </summary>
public class UtilsAdmin
{
  Label mLabMsg=null;
  
		public UtilsAdmin()
		{
				//
				// TODO: aggiungere qui la logica del costruttore
				//
		}

		public void EncryptConnString(string perc)
		{

            // Get the Web application configuration.
            System.Configuration.Configuration webConfig =
            WebConfigurationManager.OpenWebConfiguration("/mywebautoeu");


            // Get the section.
            string configPath = "system.web/cache/sqlCacheDependency";
            System.Web.Configuration.SqlCacheDependencySection sqlDs =
            (System.Web.Configuration.SqlCacheDependencySection)webConfig.GetSection(
            configPath);

            //
				Configuration config = WebConfigurationManager.OpenWebConfiguration(perc);
				ConfigurationSection section = config.GetSection("connectionStrings");
				if (!section.SectionInformation.IsProtected)
				{
						section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
						config.Save();
				}
		}
		
		public void criptaConnString()
		{
				ConnectionStringsSection cs = ConfigurationManager.GetSection("connectionStrings") as ConnectionStringsSection;
				if(cs.SectionInformation.IsProtected )return ;
				try
				{   
						//cs.SectionInformation.ForceSave = true;
						cs.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
				}
				catch (Exception ex)
				{
						try
						{
								cs.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");								
						}
						catch (Exception ex2)
						{ throw; }
						throw;
				}

		}//fM

 public void ProtectSection(string path)
{

    // Get the current configuration file.
    Configuration config = ConfigurationManager.OpenExeConfiguration(path );

    // Get the section.
				var sezione = config.GetSection("connectionStrings");

    if(sezione.SectionInformation.IsProtected )return ;
    // Protect (encrypt)the section.
    sezione.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");

    // Save the encrypted section.
    sezione.SectionInformation.ForceSave = true;

    config.Save(ConfigurationSaveMode.Full);

    // Display decrypted configuration 
    // section. Note, the system uses the Rsa provider to decrypt
    // the section transparently.
    string sectionXml =
        sezione.SectionInformation.GetRawXml();

				//Console.WriteLine("Decrypted section:");
				//Console.WriteLine(sectionXml);

}//fM


}//fC
